package com.ex.views;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class KAdapter<T extends AbsItem<?>> extends BaseAdapter {

	private List<T> mDataList;
	private List<Integer> mViewTypeList;
	
	public void setViewTypeList(List<Integer> viewTypeList){
		if(viewTypeList != null && viewTypeList.size() > 0){
			mViewTypeList = viewTypeList;
		}
	}
	
	public interface IDataUpdateListener{
		void onUpdate();
	}
	private IDataUpdateListener dataUpdateListener;
	
	public void setIDataUpdateListener(IDataUpdateListener dataUpdateListener){
		this.dataUpdateListener = dataUpdateListener;
	}
	
	public KAdapter(List<T> dataList){
		mDataList = (dataList == null) ? new LinkedList<T>() : dataList;
		initViewTypeList();
	}
	
	public KAdapter(){
		mDataList = new LinkedList<T>();
		initViewTypeList();
	}
	
	private void initViewTypeList(){
		mViewTypeList = new ArrayList<Integer>();
		mViewTypeList.add(0);
	}
	
	public List<T> getDataList(){
		return mDataList;
	}
	
	public void setData(List<T> dataList){
		mDataList = (dataList == null) ? new LinkedList<T>() : dataList;
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	@Override
	public int getViewTypeCount() {
		return mViewTypeList.size();
	}
	
	@Override
	public int getItemViewType(int position) {
		return mDataList.get(position).getViewType();
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public T getItem(int position) {
		return mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return mDataList.get(position).onGetView(convertView, position, parent);
	}
	
	public void addData(T data){
		if(data == null){
			return;
		}
		mDataList.add(data);
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	public void addData(T data, int pos){
		if(data == null){
			return;
		}
		if(pos > mDataList.size()){
			return;
		}
		mDataList.add(pos, data);
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	public void removeData(T data){
		if(data == null){
			return;
		}
		mDataList.remove(data);
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	public void addDataList(List<T> dataList){
		if(dataList == null || dataList.size() == 0){
			return;
		}
		mDataList.addAll(dataList);
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	public void removeDataList(List<T> dataList){
		if(dataList == null || dataList.size() == 0){
			return;
		}
		Iterator<T> iterator = mDataList.iterator();
		Iterator<T> iteratorDelete = dataList.iterator();
		T temp = null;
		while(iterator.hasNext()){
			temp = iterator.next();
			while(iteratorDelete.hasNext()){
				if(temp == iteratorDelete.next()){
					iterator.remove();
					iteratorDelete.remove();
					break;
				}
			}
		}
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}
	
	public void clear() {
		mDataList.clear();
		notifyDataSetChanged();
		if(dataUpdateListener != null){
			dataUpdateListener.onUpdate();
		}
	}

}
